
'''
自定义方法
'''

from decimal import Decimal
from common.config import *
import json
class Helper( object ):

    def __init__(self):
        pass
    # 价格保留两位小数
    @staticmethod
    def decimalParse( data ):

        totalPrice = Decimal(str(data)).quantize(Decimal('0.00'))

        return totalPrice
    # 查询数据方法
    @staticmethod
    def selectData(TABLE,MYSQL):
        MYSQL.cursor.execute(SELECT_SQL.format( TABLE = TABLE ))
        result = MYSQL.cursor.fetchall()
        return result
    #合并数据库
    @staticmethod
    def mergeTable(TABLE=None,MYSQL=None,DATA=None):
        STRING = ""; VALUE = ""
        for k in DATA:

            STRING += '`'+k+'`,'
            VALUE +=  '"{' + k + '}",'

        per = '(' + STRING.strip(",") + ')'
        val = '(' + VALUE.strip(",") + ')'
        # 原生SQL
        ORIGIN = 'INSERT INTO `' + TABLE_MEDICAL + '`'+per+'VALUES'+val+''

        INSERT_SQL = ORIGIN.format( **DATA )

        print( INSERT_SQL )

        MYSQL.cursor.execute( INSERT_SQL )
        MYSQL._conn.commit()
    # 格式化产品信息
    @staticmethod
    def formatProductInfo( KEY = None,VALUE=None ):
        # 定义空变量
        STRING=""
        for k,v  in zip(KEY,VALUE):
            STRING += '"' + k  +  '"' +  ":" + '"' + v + '"' + ','
        JSON = '{' + STRING.strip(",") + "}"
        return json.loads( JSON )
